package com.wgl.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * @author wgl
 * @version 1.0.0
 * @ClassName CorsConfig.java
 * @Description TODO
 * @createTime 2022年02月08日 22:01:00
 */
@Configuration
public class CorsConfig {

    /**
    * @Author: Wgl
    * @Description: 统一处理跨域问题
    * @Date: 2022/2/8 22:01;
    ;
    * @return: org.springframework.web.cors.reactive.CorsWebFilter;
    */
    @Bean
    public CorsWebFilter corsWebFilter(){
        CorsConfiguration configuration = new CorsConfiguration();
        //添加允许的请求标识
        configuration.addAllowedHeader("*");
        //添加允许的方法
        configuration.addAllowedMethod("*");
        //添加允许的来源
        configuration.addAllowedOrigin("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        // 注册 Cors 配置   /** 使用请求都遵循该规则
        source.registerCorsConfiguration("/**",configuration);

        return new CorsWebFilter(source);
    }
}
